﻿using Devices;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DevicesView.Logic.Log;
using DevicesView.Model.ViewData;
using Newtonsoft.Json;
using DevicesView.Entity.ViewData;
using DevicesView.Logic.App;
using BCL.Tool.Extend;
namespace DevicesView.Logic.Print
{
    public class PrintLogic
    {
        private static string configPath;
        private static Dictionary<string, byte[]> images;

        static PrintLogic()
        {
            configPath = AppDomain.CurrentDomain.BaseDirectory + "Devices\\printConfig\\";
            if (!Directory.Exists(configPath))
                Directory.CreateDirectory(configPath);
        }
        public static Dictionary<string, IPrint> GetPrintConfig()
        {
            string[] files = Directory.GetFiles(configPath, "*.xml");
            if (files == null || files.Length == 0)
                return null;
            Dictionary<string, Devices.IPrint> dir = new Dictionary<string, Devices.IPrint>();
            foreach (var item in files)
            {
                try
                {
                    Devices.PrintInfo pinfo = Tool.GetObjectByXML<Devices.PrintInfo>(item);
                    Devices.IPrint iprint = Tool.GetObjectByClass<Devices.IPrint>(pinfo.Dll, pinfo.ClassName);
                    dir.Add(Path.GetFileNameWithoutExtension(item), iprint);
                }
                catch (Exception)
                {
                }
            }
            return dir;
        }

        public static IPrint GetPrintConfig(Devices.DevicesInformation info)
        {
            string xmlname = configPath + info.Code + ".xml";
            Devices.PrintInfo pinfo = new Devices.PrintInfo(true);
            if (!File.Exists(xmlname))
                return Tool.GetObjectByClass<IPrint>(pinfo.Dll, pinfo.ClassName);

            return GetPrintConfig(Tool.GetObjectByXML<PrintInfo>(xmlname));
        }


        public static IPrint GetPrintConfig(PrintInfo pinfo)
        {
            return Tool.GetObjectByClass<IPrint>(pinfo.Dll, pinfo.ClassName);
        }
        //private static string httpAdd = "http://localhost:5921/api/";
        public static List<PrintInfo> GetPrints()
        {
            //var dy = new { };
            //HttpItem hi = new HttpItem { Url = httpAdd + "printinfo/queryall", Method = HttpMethod.Post, ContentType = "application/json" };
            //hi.Content = JsonConvert.SerializeObject(dy).GetBytes(UTF8Encoding.UTF8);
            //HttpResult<List<PrintInfo>> httpResult = WebLogic.GetHttpResult<List<PrintInfo>>(hi);

            //if (!httpResult.Success || httpResult.Result == null || httpResult.Result.Count == 0)
            //    return null;

            //return httpResult.Result;

            //var aa = new PrintInfo()
            //{
            //    Author = "咸菜",
            //    Name = "基础打印",
            //    Describe = "基础打印",
            //    Dll = "Print.dll",
            //    ClassName = "Devices.Print.UniversalPrint",
            //    UsableDeviceType = DeviceType.MaxAndMin,
            //    Images = new List<string>() {"print.jpg"}
            //};
            //BCLLog hisLog=new BCLLog();
            //hisLog.Msg(JsonConvert.SerializeObject(aa));


            List<PrintInfo> printInfos = HttpRequest.HttpPost<PrintInfo>(WebApi.Query_PrintInfo);
            //HttpResult<List<PrintInfo>> pinofs = WebLogic.GetHttpResult < List<PrintInfo>>(Url = httpAdd + "PrintInfo/QueryAll", Method = HttpMethod.Post, ContentType = "application/json" );
            //List<PrintInfo> prints = new List<PrintInfo>();
            //prints.Add(new PrintInfo());
            //prints.Add(new PrintInfo { Name = "带医院LOGO", Dll = "DevicesView.Print.dll", ClassName = "DevicesView.Print.HospitalPrint" });
            return printInfos;
        }

        public static System.Drawing.Image GetImage(string path)
        {
            if (images == null)
                images = new Dictionary<string, byte[]>();
            if (!images.ContainsKey(path))
                images.Add(path, File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "image\\" + path));
            if (!images.ContainsKey(path))
                return null;
            return System.Drawing.Image.FromStream(new MemoryStream(images[path]));
        }

        public static System.Windows.Media.ImageSource GetBitImage(string path)
        {
            if (images == null)
                images = new Dictionary<string, byte[]>();
            if (!images.ContainsKey(path))
                images.Add(path, File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "image\\" + path));
            if (!images.ContainsKey(path))
                return null;
            return images[path].ToBitmap();
        }

        /// <summary>
        /// 保存打印信息
        /// </summary>
        /// <param name="info"></param>
        /// <param name="pinfo"></param>
        public static void SavePrintConfig(Devices.DevicesInformation info, Devices.PrintInfo pinfo)
        {
            Tool.ObjectSaveToXML(pinfo, configPath + info.Code + ".xml");
        }


        public static List<TestItemInfo> GetTestItems()
        {
            string ServerUrl = AppResource.LogicServerAddress + "testitem/all";
            HttpResult<List<TestItemInfo>> obj = WebLogic.GetHttpResult<List<TestItemInfo>>(new HttpItem { Url = ServerUrl, Method = HttpMethod.Get });
            if (!obj.Success)
                return null;
            return obj.Result;
        }
    }



}
